﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MindASPCoreWebAPI.ApiProject.Controllers;
using MindASPCoreWebAPI.BaseLibrary.Tools.FileTools;
using MindASPCoreWebAPI.DtoModel.Common;
using MindASPCoreWebAPI.DtoModel.FileManager;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MindASPCoreWebAPI.ApiProject.Area.FileManager
{
    [Route("api/FileManager/[controller]/[action]")]
    public class FileManagerController : MyBaseController
    {
        static string configPath = "";
        public FileManagerController(IConfiguration configuration)
        {
            if(string.IsNullOrEmpty(configPath))
            {
                configPath = configuration.GetSection("MyConfig")["FileUploadPath"];
            }
        }
        [HttpPost]
        public async Task<ResultModel<List<FileUpLoadResult>>> FilesUpload(List<IFormFile> fileList)
        {
            ResultModel<List<FileUpLoadResult>> result = new ResultModel<List<FileUpLoadResult>>();
            try
            {
                List<FileUpLoadResult> fulList = new List<FileUpLoadResult>();
                if (fileList != null && fileList.Count > 0)
                {
                    foreach (var iFormFile in fileList)
                    {
                        FileUpLoadResult ful = new FileUpLoadResult();
                        string fileName = Guid.NewGuid() + Path.GetExtension(iFormFile.FileName);
                        string urlPath = configPath + "/" + fileName;
                        string upLoadPath = FileTools.MapPath("/wwwroot" + configPath);
                        ful.FileOldName = iFormFile.FileName;
                        ful.FileNewName = fileName;
                        ful.FileNewPath = urlPath;
                        ful.FileSize = iFormFile.Length;
                        ful.FileType = iFormFile.ContentType;
                        if (!Directory.Exists(upLoadPath))
                        {
                            Directory.CreateDirectory(upLoadPath);
                        }
                        try
                        {
                            using (var fs = new FileStream(upLoadPath + "/" + fileName, FileMode.Create))
                            {
                                await iFormFile.CopyToAsync(fs);
                                fs.Flush();
                                ful.IsUpload = true;
                            }
                        }
                        catch (Exception ex)
                        {

                            ful.IsUpload = false;
                        }
                        fulList.Add(ful);
                    }
                    result.code = ResultCode.Ok;
                    result.data = fulList;
                    result.mess = "成功";
                }
                else
                {
                    result.code = ResultCode.Ok;
                    result.data = null;
                    result.mess = "失败";
                }
            }
            catch (Exception ex)
            {

                result.code = ResultCode.Error;
                result.mess = "错误";
            }
            return result;
        }
    }
}
